﻿using OSSUtility.Communicate;
using OSSUtility.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.Device
{

    /// <summary>
    /// 超声
    /// </summary>
    public class DevUltrasound : DevBase
    {
        public DevUltrasound(int CommType, string FSrvIP, int FPort) : base(CommType, FSrvIP, FPort)
        {

        }



        #region  继电器

        /// <summary>
        /// 继电器开
        /// </summary>
        /// <returns></returns>
        public bool OpenFan()
        {
            bool temperature = false;

            try
            {
                // 01 06 00 00 00 01 48 0A
                // 01 06 00 01 00 01 19 CA
                // 01 06 00 02 00 01 E9 CA
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0000;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x01;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }

        /// <summary>
        /// 继电器关
        /// </summary>
        /// <returns></returns>
        public bool CloseFan()
        {

            bool temperature = false;


            try
            {
                //01 06 00 01 00 01 19 CA
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0x01;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x01;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }
        #endregion






        #region  氮吹阀门

        /// <summary>
        /// 氮吹阀门开
        /// </summary>
        /// <returns></returns>
        public bool OpenDCF(int Type)
        {
            bool temperature = false;

            try
            {
                // 01 06 00 00 00 01 48 0A
                // 01 06 00 01 00 01 19 CA
                // 01 06 00 02 00 01 E9 CA
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0000;

                if (Type == 1)
                {
                    startAddr = 0000;

                }

                if (Type == 2)
                {
                    startAddr = 0001;

                }

                if (Type == 3)
                {
                    startAddr = 0002;

                }


                if (Type == 4)
                {
                    startAddr = 0003;

                }


                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x01;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }

        /// <summary>
        /// 氮吹阀门关
        /// </summary>
        /// <returns></returns>
        public bool CloseDCF(int Type)
        {

            bool temperature = false;


            try
            {
                //01 06 00 00 00 00 89 CA
                //01 06 00 01 00 00 D8 0A
                //01 06 00 02 00 00 28 0A
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0x00;

                if (Type == 1)
                {
                    startAddr = 0000;

                }

                if (Type == 2)
                {
                    startAddr = 0001;

                }

                if (Type == 3)
                {
                    startAddr = 0002;

                }
                if (Type == 4)
                {
                    startAddr = 0003;

                }

                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x00;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }
        #endregion
    }
}
